<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>table 组件综合演示 - Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--引入layui样式-->
    <link href="/layui/css/layui.css" rel="stylesheet">
</head>
<body>

<!--查询的表单  -->
<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">订单状态</label>
            <div class="layui-input-inline" style="width: 100px;">
                <select name="states" class="states" >
                    <option value="">请选择</option>
                    <option value="0">待支付</option>
                    <option value="1">已支付</option>
                    <option value="2">已完成</option>
                    <option value="3">已取消</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">餐桌</label>
            <div class="layui-input-inline" style="width: 100px;">
                <select name="deskId" class="deskId" >
                    <option value="">请选择</option>
                    <!-- 向服务端发请求获取 -->
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">时间区间</label>
            <div class="layui-input-inline" style="width: 400px;">
                <input type="datetime-local" name="timeMin" class="layui-input" style="width: 180px; display: inline-block" />
                ~
                <input type="datetime-local" name="timeMax" class="layui-input" style="width: 180px; display: inline-block" />
            </div>
        </div>
        <div class="layui-inline">
            <button type="submit" class="layui-btn" lay-submit lay-filter="selectBtn">搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--查询的表单结束-->


<!--表格容器-->
<div style="padding: 16px; display: none"id="orderDetailDiv">
    <table class="layui-hide" id="orderDetail" lay-filter="orderDetail"></table>
</div>


<!--表格容器-->
<div style="padding: 16px;">
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>




<!--行内工具条-->
<script type="text/html" id="toolDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs " lay-event="final">完成</a>
        <a class="layui-btn layui-btn-xs layui-bg-orange" lay-event="canl">取消</a>
        <a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="detail">详情</a>
    </div>
</script>

<!--引入layui的脚步-->
<script src="/layui/layui.js"></script>
<script>
    layui.use(['table', 'dropdown'], function(){
        var table = layui.table;
        var dropdown = layui.dropdown;
        var $ = layui.$;
        var form = layui.form;


        //发请求，获取"餐桌列表"，渲染在页面的下拉框 class="deskId"
        $.get("/desk/getAllList",function(res){
            for (let t of res.data) {
                $(".deskId").append(`<option value="${t.id}">${t.dname}</option>`)
            }
            form.render('select'); // 仅渲染 select 元素
        },"json")


        // 表单的提交事件监听--------搜索
        form.on('submit(selectBtn)', function(data){
            // console.log(data.field)
            table.reload("test",{where:data.field}); //表格重载
            return false; // 阻止默认 form 跳转
        });








        // 表格渲染
        table.render({
            elem: '#test',
            url: '/orders/', // 请求接口！
            page: true,
            cols: [[   //表头！
                {type: 'checkbox', fixed: 'left'},
                {field:'id', fixed: 'left', width:50, title: 'ID', sort: true, totalRow: '合计：'},
                {field:'deskId', title: '餐桌id'},
                {field:'dname', title: '餐桌名'},
                {field:'propleCount', title: '用餐人数'},
                {field:'descr', title: '备注'},
                {field:'total', title: '总金额'},
                {field:'states', title: '订单状态',templet: function(d){
                        if(d.states==0){
                            return '待支付'
                        }else if(d.states==1){
                            return '已支付'
                        }else if(d.states==2){
                            return '已完成'
                        }else if(d.states==3){
                            return '已取消'
                        }
                    }
                },
                {field:'orderTime', title: '下单时间'},
                {field:'payTime', title: '支付时间'},
                {field:'finialTime', title: '完成时间'},
                {field:'calTime', title: '取消时间'},
                {fixed: 'right', title:'操作', width: 160, minWidth: 125, templet: '#toolDemo'}
            ]]
        });







        // 行内工具栏事件监听
        table.on('tool(test)', function(obj){ // 双击 toolDouble
            var data = obj.data; // 获得当前行数据
            // console.log(obj)
            if(obj.event === 'final'){
                //完成
                layer.confirm('确定[id: '+ data.id +'] 号订单已完成了？', function(index){
                    var field = {id:data.id,states:2}
                    //向服务端发送编辑请求
                    $.ajax({
                        url:"/orders/",
                        method:"put",
                        contentType: 'application/json',
                        data: JSON.stringify(field) ,
                        dataType:"json",
                        success:function(res){
                            if (res.code==200){
                                layer.closeAll();
                                table.reload("test",{})
                            }else{
                                layer.msg(res.msg)
                            }
                        }
                    })
                })
            } else if(obj.event === 'canl'){
                //取消
                layer.confirm('确定要取消 [id: '+ data.id +'] 号订单么', function(index){
                    var field = {id:data.id,states:3}
                    //向服务端发送编辑请求
                    $.ajax({
                        url:"/orders/",
                        method:"put",
                        contentType: 'application/json',
                        data: JSON.stringify(field) ,
                        dataType:"json",
                        success:function(res){
                            if (res.code==200){
                                layer.closeAll();
                                table.reload("test",{})
                            }else{
                                layer.msg(res.msg)
                            }
                        }
                    })

                });

            } else if(obj.event === 'detail'){
                //订单详情
                table.render({
                    elem: '#orderDetail',
                    url: '/order-detail/?orderId='+data.id, // 请求接口！
                    cols: [[   //表头！
                        {field:'id', title: 'id'},
                        {field:'orderId', title: '所属订单'},
                        {field:'proName', title: '菜品名'},
                        {field:'proPrice', title: '菜品价格'},
                        {field:'proImg', title: '图片',templet:'<div><img src="/showPic/{{d.proImg}}"/></div>'},
                        {field:'num', title: '数量'},
                    ]]
                })
                //弹窗
                layer.open({
                    title:data.id+'号订单',
                    type:1,
                    area:['80%','80%'],
                    shadeClose:true,
                    content:$('#orderDetailDiv')

                })

            }
        });





    });
</script>
</body>
</html>